using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    public class WorkViewController : ControllerBase
    {
        private readonly IRepository<Course> _Course;
        private readonly IRepository<Students> _Students;
        private readonly IRepository<OutStanding> _OutStanding;
        private readonly IRepository<Term> _term;

        public WorkViewController(IRepository<Course> Course, IRepository<Students> Students, IRepository<OutStanding> OutStanding,IRepository<Term> term)
        {
            _Course = Course;
            _Students = Students;
            _OutStanding = OutStanding;
            _term=term;
        }

        //添加作品
        [HttpPost("/AddStuWorkView")]
        public async Task<object> AddStuWorkView(OutStanding outStanding)
        {

            //作品表
            var Standing = await _OutStanding.AddAsync(outStanding);
            return new
            {
                code = 200,
                msg = "添加作品成功",
                data = Standing
            };
        }
        [HttpGet("/StuWorkView")]
        public async Task<object> StuWorkView()
        {
            //作品表
            var OutStanding = _OutStanding.Table.ToList();
            var SelectStandings = OutStanding.Select(x => new
            {
                x.Title,
                x.Text,
                x.studentName,
                x.CreatedAt,
                x.Id,
            });
            List<object> SelectStanding = new List<object>();

            foreach (var item in SelectStandings)
            {
                var PreviewArticle = item.Text.Substring(0, item.Text.Length);
                if (item.Text.Length >= 20)
                {
                    PreviewArticle = item.Text.Substring(0, 20);
                    SelectStanding.Add(new
                    {
                        Id = item.Id,
                        studentName = item.studentName,
                        CreatedAt = item.CreatedAt,
                        Text = item.Text,
                        Title = item.Title,
                        PreviewArticle = PreviewArticle
                    });
                }
                else
                {
                    SelectStanding.Add(new
                    {
                        Id = item.Id,
                        studentName = item.studentName,
                        CreatedAt = item.CreatedAt,
                        Text = item.Text,
                        Title = item.Title,
                        PreviewArticle = PreviewArticle
                    });
                }

            }

            if (SelectStandings != null)
            {
                return new
                {
                    code = 200,
                    msg = "查询成功",
                    data = SelectStanding
                };
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "数据为空",
                    data = ""
                };
            }

        }

        //删除作品
        [HttpDelete("/DelWork/{WorkId}")]
        public async Task<object> DelWork(Guid WorkId)
        {

            var DelWorkData = await _OutStanding.GetByIdAsync(WorkId);
            if (DelWorkData != null)
            {
                await _OutStanding.DeleteAsync(WorkId, true);
                return new
                {
                    code = 200,
                    msg = "删除作品成功！！！",
                    data = "null"
                };
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "为找到此作品id",
                    data = "null"
                };
            }

        }
        //根据id查找作品的详细内容
        [HttpGet("/StuWorkView/{id}")]
        public async Task<object> QueryText(Guid id)
        {

            var work = await _OutStanding.GetByIdAsync(id);
            if (work != null)
            {
                return new
                {
                    code = 200,
                    msg = "查找成功",
                    data = work
                };
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "为找到此作品id",
                    data = "null"
                };
            }

        }
        //根据id查找对应学期的id
        [HttpGet("/disPlayTerm/{id}")]
        public async Task<object> disPlayTerm(Guid id)
        {

            var term = await _term.GetByIdAsync(id);
            if (term != null)
            {
                return new
                {
                    code = 200,
                    msg = "查找成功",
                    data = term
                };
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "查找失败",
                    data = "null"
                };
            }

        }
    }

}